package com.xlhj.yucloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlhj.yucloud.entity.BaseEntity;
import com.xlhj.yucloud.entity.SysRoleInfo;
import com.xlhj.yucloud.entity.SysRoleMenu;
import com.xlhj.yucloud.entity.SysUserRole;
import com.xlhj.yucloud.entity.enums.StatusEnum;
import com.xlhj.yucloud.mapper.SysRoleInfoMapper;
import com.xlhj.yucloud.mapper.SysRoleMenuMapper;
import com.xlhj.yucloud.mapper.SysUserRoleMapper;
import com.xlhj.yucloud.service.SysRoleInfoService;
import com.xlhj.yucloud.util.DateUtils;
import com.xlhj.yucloud.web.model.RoleInfoReqDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: liucaijing
 * @description: SysRoleInfoServiceImpl
 * @date: 2021/9/9 21:40
 */
@Service
public class SysRoleInfoServiceImpl extends ServiceImpl<SysRoleInfoMapper, SysRoleInfo> implements SysRoleInfoService {

    @Autowired
    private SysRoleInfoMapper roleInfoMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    public Set<String> getRolePermsByUserId(Long userId) {
        List<String> permList = roleInfoMapper.getRolePermsByUserId(userId);
        Set<String> permSet = new HashSet<>();
        permSet.addAll(permList);
        return permSet;
    }

    @Override
    public Page<SysRoleInfo> selectRoleInfoPageList(RoleInfoReqDto reqDto) {
        Page<SysRoleInfo> roleInfoPage = new Page<>(reqDto.getPageNum(), reqDto.getPageSize());
        LambdaQueryWrapper<SysRoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(reqDto.getRoleName())) {
            queryWrapper.like(SysRoleInfo::getRoleName, reqDto.getRoleName());
        }
        if ((StringUtils.hasLength(reqDto.getRoleKey()))) {
            queryWrapper.like(SysRoleInfo::getRoleKey, reqDto.getRoleKey());
        }
        if (!ObjectUtils.isEmpty(reqDto.getStatus())) {
            queryWrapper.eq(SysRoleInfo::getStatus, reqDto.getStatus());
        }
        if (StringUtils.hasLength(reqDto.getBeginDate()) && StringUtils.hasLength(reqDto.getEndDate())) {
            queryWrapper.ge(BaseEntity::getCreateTime, DateUtils.getLocalDateTimeFromDateStr(reqDto.getBeginDate(), LocalTime.MIN));
            queryWrapper.le(BaseEntity::getCreateTime, DateUtils.getLocalDateTimeFromDateStr(reqDto.getEndDate(), LocalTime.MAX));
        }
        return this.page(roleInfoPage, queryWrapper);
    }

    @Override
    public void deleteRoleByIds(Long[] roleIds) {
        for (Long roleId : roleIds) {
            SysRoleInfo roleInfo = new SysRoleInfo();
            roleInfo.setId(roleId);
            this.removeById(roleInfo);
            LambdaQueryWrapper<SysUserRole> userRoleQueryWrapper = new LambdaQueryWrapper<>();
            userRoleQueryWrapper.eq(SysUserRole::getRoleId, roleId);
            userRoleMapper.delete(userRoleQueryWrapper);
            LambdaQueryWrapper<SysRoleMenu> roleMenuQueryWrapper = new LambdaQueryWrapper<>();
            roleMenuQueryWrapper.eq(SysRoleMenu::getRoleId, roleId);
            roleMenuMapper.delete(roleMenuQueryWrapper);
        }
    }

    @Override
    public void changeRoleStatus(Long roleId, Integer status, String userName) {
        SysRoleInfo roleInfo = new SysRoleInfo();
        roleInfo.setId(roleId);
        roleInfo.setStatus(status);
        roleInfoMapper.updateById(roleInfo);
    }

    @Override
    public Long checkRoleNameUnique(String roleName) {
        LambdaQueryWrapper<SysRoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleInfo::getRoleName, roleName)
                .eq(SysRoleInfo::getStatus, StatusEnum.NORMAL);
        Long count = roleInfoMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public Long checkRoleKeyUnique(String roleKey) {
        LambdaQueryWrapper<SysRoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleInfo::getRoleKey, roleKey)
                .eq(SysRoleInfo::getStatus, StatusEnum.NORMAL);
        Long count = roleInfoMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<Long> selectRoleListByUserId(Long userId) {
        return roleInfoMapper.selectRoleListByUserId(userId);
    }

    @Override
    public List<SysRoleInfo> selectAllRoleInfo() {
        LambdaQueryWrapper<SysRoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleInfo::getStatus, StatusEnum.NORMAL);
        return roleInfoMapper.selectList(queryWrapper);
    }
}
